<!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8">
    <title>修改学员信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">修改学员信息</div>
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" action="" lay-filter="component-form-group" id="component-form-group">
                    <div class="layui-form-item">
                        <label class="layui-form-label">id</label>
                        <div class="layui-input-inline">
                            <input type="text" name="id" lay-verify="id" id="sid" readonly="readonly" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">名字</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" lay-verify="name" autocomplete="off" id="name" placeholder="请输入名字" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">验证手机</label>
                            <div class="layui-input-inline">
                                <input type="tel" name="phone" lay-verify="required|phone" id="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                    </div>

                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">生日</label>
                            <div class="layui-input-inline">
                                <input type="text" name="date" id="LAY-component-form-group-date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">便签选择</label>
                        <div class="layui-input-block">
                            <select name="studentType" lay-filter="aihao" id="studentType">
                             
                          </select>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" id="sex1" name="sex" value="男" title="男">
                            <input type="radio" id="sex2" name="sex" value="女" title="女">

                        </div>
                    </div>

                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;">
                                <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">保存</button>

                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="../../../layuiadmin/jquery/jquery-1.11.1.min.js"></script>
    <script src="../../../layuiadmin/layui/layui.js"></script>
    <script src="../../../layuiadmin/url.js"></script>
    <script>
        //
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }

        layui.config({
            base: '../../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'laydate'], function() {
            var $ = layui.$,
                admin = layui.admin,
                element = layui.element,
                layer = layui.layer,
                laydate = layui.laydate,
                form = layui.form;

            $(function() {



                //获取到了url上的id

                var sid = getQueryVariable("sid");

                $.ajax({
                    url: applicationUrl + 'student/api/byid',
                    type: "GET",
                    dataType: "json",
                    data: {
                        sid: sid
                    },
                    success: function(data) {
                        $("#sid").val(data.data.sid);
                        $("#name").val(data.data.name);
                        $("#phone").val(data.data.phone);
                        $("#LAY-component-form-group-date").val(data.data.birthday);

                        $("#studentType").append("<option value='" + data.data.studentType + "'>" + data.data.typeStudent.names + "</option>");


                        form.render(); //注意渲染页面表单，否则不显示数据
                        getstudentTypeList();
                        console.log(data.data.sex);
                        if (data.data.sex == "男") {
                            $("#sex1").prop("checked", true);

                        } else if (data.data.sex == "女") {
                            $("#sex2").prop("checked", true);
                        }

                    }
                });



            });

            function getstudentTypeList() {
                $.ajax({
                    url: applicationUrl + 'studentType/api/list',
                    type: "POST",
                    dataType: "json",
                    success: function(data) {

                        for (var i = 0; i < data.length; i++) {
                            $("#studentType").append("<option value='" + data[i].studentType + "'>" + data[i].names + "</option>");

                        }
                        form.render(); //注意渲染页面表单，否则不显示数据
                    }
                });
            }
            form.render(null, 'component-form-group');

            laydate.render({
                elem: '#LAY-component-form-group-date'
            });

            /* 自定义验证规则 */
            form.verify({
                name: function(value) {
                    if (value.length < 2) {
                        return '标题至少得2个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                content: function(value) {
                    layedit.sync(editIndex);
                }
            });

            /* 监听指定开关 */
            form.on('switch(component-form-switchTest)', function(data) {
                layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                    offset: '6px'
                });
                layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
            });

            /* 监听提交 */
            form.on('submit(component-form-demo1)', function(data) {
                var sid = getQueryVariable("sid");
                var name = data.field.name;
                var sex = data.field.sex;
                var studentType = data.field.studentType;
                var date = data.field.date;
                var phone = data.field.phone;
                console.log(data.field);
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        sid: sid,
                        name: name,
                        sex: sex,
                        phone: phone,
                        birthday: date,
                        studentType: studentType
                    },
                    url: applicationUrl + 'student/api/update',
                    success: function(data) {
                        layer.msg(data.msg);


                        layui.form.render();

                        window.opener = null; //关闭窗口时不出现提示窗口
                        window.close();
                    },
                    error: function(data) {
                        layer.msg(data.msg);

                    }
                })

                return false;
            });
        });
    </script>
</body>

</html>